package com.tanxi521.tnweb.controller;

import com.tanxi521.tnweb.model.AllUserInfo;
import com.tanxi521.tnweb.model.AllUserPageList;
import com.tanxi521.tnweb.model.IndexFootInfo;
import com.tanxi521.tnweb.model.WebUser;
import com.tanxi521.tnweb.service.PlayerAccountService;
import com.tanxi521.tnweb.service.WebUserService;
import com.tanxi521.tnweb.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    WebUserService webUserService;

    @Autowired
    PlayerAccountService playerAccountService;

    @RequestMapping("index")
    public String toIndex() {
        return "index";
    }

    @RequestMapping("register")
    public String reg() {
        return "register";
    }

    @RequestMapping("login")
    public String log() {
        return "login";
    }

    @RequestMapping("index/footInfo")
    @ResponseBody
    public IndexFootInfo getInfo() {
        IndexFootInfo indexFootInfo = new IndexFootInfo();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = format.parse("2018-10-18");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        assert time != null;
        long start = time.getTime();
        long now = new Date().getTime();
        long days = (now - start) / 1000 / 60 / 60 / 24;
        indexFootInfo.setServerDays((int) days);
        indexFootInfo.setAllPlayers(playerAccountService.getPlayerCount());
        indexFootInfo.setOnlineNums(playerAccountService.getOnlineNums());
        indexFootInfo.setWebUserNums(webUserService.getWebUserNums());
        return indexFootInfo;
    }

    @RequestMapping("/user/playerInfo")
    public String toPlayerInfo(HttpSession session) {
        WebUser webUser = (WebUser) session.getAttribute("webUser");
        if (webUser != null) {
            session.setAttribute("playerInfoId", webUser.getId());
            return "playerinfo";
        }
        session.setAttribute("playerInfoId", 0);
        return "playerinfo";
    }

    @RequestMapping("/user/otherPlayerInfo")
    public String toOtherPlayerInfo(Integer id, HttpSession session) {
        session.setAttribute("playerInfoId", id);
        return "playerinfo";
    }

    @RequestMapping("/user/allUser")
    public String toAllUser() {
        return "alluser";
    }

    @RequestMapping("/user/allUser/list")
    @ResponseBody
    public AllUserPageList listAllUser(Long cunPage) {
        PageInfo pageInfo = new PageInfo(8);
        long count = webUserService.getWebUserNums();
        pageInfo.setTotalCount(count);

        if (cunPage==null||cunPage < 1) {
            cunPage = 1l;
        }
        Long lastPage = pageInfo.getLastPage();
        if (cunPage > lastPage) {
            cunPage = lastPage;
        }
        pageInfo.setCunPage(cunPage);
        List<AllUserInfo> allUserInfos =  webUserService.queryAllUsers(pageInfo);
        AllUserPageList allUserPageList = new AllUserPageList();
        allUserPageList.setAllUserInfos(allUserInfos);
        allUserPageList.setPageInfo(pageInfo);
        return allUserPageList;
    }
}
